import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutterlib/common/utils/utils.dart';

import '../widgets.dart';


class HeartDialog extends StatefulWidget {
  const HeartDialog({
    Key key,
    this.text,
  }) : super(key: key);

  final String text;

  @override
  State<StatefulWidget> createState() => HeartDialogState();

  static void show(
    BuildContext context, {
    String text,
  }) {
    showDialog<void>(
      context: context,
      builder: (_) => WillPopScope(
        onWillPop: () => new Future.value(false),
        // onWillPop: (){
        //   Navigator.pop(context);
        // },
        child: HeartDialog(
        text: text,
      ),),
    );
  }
}

class HeartDialogState extends State<HeartDialog> {
  Duration _duration = Duration(microseconds: 1500);
  String _type, _text;
  VoidCallback _customPop;
  Widget _icon = const SpinKitHeartWidget();

  @override
  void initState() {
    super.initState();
    _text = widget.text;
    if (mounted) {
      setState(() {});
    }
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
  }

  @override
  void didUpdateWidget(HeartDialog oldWidget) {
    super.didUpdateWidget(oldWidget);
  }



  @override
  Widget build(BuildContext context) {
    Future<void>.delayed(_duration, () {
      try {
        if (_customPop != null) {
          _customPop();
        } else {
          Navigator.pop(context);
        }
      } catch (e) {
        trueDebugPrint('Error when running pop in loading dialog: $e');
      }
    });
    Widget child = Center(
      child: SizedBox.fromSize(
        size: Size.square(180.w),
        child: DecoratedBox(
          decoration: BoxDecoration(
            color: Theme.of(context).canvasColor,
            borderRadius: BorderRadius.circular(8.w),
          ),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              SizedBox.fromSize(
                size: Size.square(50.w),
                child: Center(child: _icon),
              ),
              Container(
                margin: EdgeInsets.only(top: 40.h),
                child: Text(
                  _text,
                  style: TextStyle(fontSize: 16.sp),
                ),
              ),
            ],
          ),
        ),
      ),
    );

    child = Material(type: MaterialType.transparency, child: child);

    return WillPopScope(onWillPop: () async => false, child: child);
  }
}

